﻿package at.geoathome.utils.logging.sos
{
	import flash.net.XMLSocket;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	/**
	 * ...
	 * @author Georg Kamptner
	 */
	public class log
	{
		// -- fields ----------------------------------------
		private static var _xmlSocket:XMLSocket = new XMLSocket();
		private static var _connected:Boolean = false;
		private static var _cmdQueue:String = "";
		
		public static const DEBUG:int = 0;
		public static const INFO:int = 1;
		public static const WARNING:int = 2;
		public static const ALERT:int = 3;
		public static const ERROR:int = 4;
		
		// -- constructor -----------------------------------
		public function log()
		{
			throw new Error("'geo.sos.log' is a static class no instances allowed, use log.*!");
		}
		
		// -- methods ---------------------------------------
		public static function c(host:String = 'localhost', port:int = 4444):Boolean
		{
			return log.connect(host, port);
		}
		
		public static function connect(host:String = 'localhost', port:int = 4444):Boolean
		{
			if(host == "") return false;
			if(port < 0) return false;
			
			// if connected close connection
			if(log._connected) log._xmlSocket.close();
			
			// start connection
			log._xmlSocket.connect(host, port);
			log._xmlSocket.addEventListener(Event.CONNECT, log.onConnect);
			log._xmlSocket.addEventListener(IOErrorEvent.IO_ERROR, log.onIOError);
			log._xmlSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, log.onSecurityError);
			return true;
		}
		
		public static function onIOError(e:Event):void
		{
		}
		
		public static function onSecurityError(e:Event):void
		{
		}
		
		public static function onConnect(e:Event):void
		{
			log._connected = true;
			log._xmlSocket.send("!SOS<show/>");
			log._xmlSocket.send("!SOS<clear/>");
			log._xmlSocket.send("!SOS<setKey><name>0</name><color>"+0xffffff+"</color></setKey>");
			log._xmlSocket.send("!SOS<setKey><name>1</name><color>"+0xe1ffc6+"</color></setKey>");
			log._xmlSocket.send("!SOS<setKey><name>2</name><color>"+0xf9ffcf+"</color></setKey>");
			log._xmlSocket.send("!SOS<setKey><name>3</name><color>"+0xffe5c6+"</color></setKey>");
			log._xmlSocket.send("!SOS<setKey><name>4</name><color>"+0xffc6c6+"</color></setKey>");
			// execute all commands that were cast before the connection has been established.
			log._xmlSocket.send(log._cmdQueue);
		}
		
		public static function s(msg:* = "", type:int = log.INFO):void
		{
			log.send(String(msg), type);
		}
		
		public static function send(msg:* = "", type:int = log.INFO):void
		{
			// if not connected -> auto connect
			if(!log._connected) log.connect();
			
			// send msg
			var cmd:String = "!SOS<showMessage key='" + type + "'><![CDATA[" + String(msg) + "]]></showMessage>" + String.fromCharCode(0);
			if(!_connected)
			{
				_cmdQueue += cmd;
			}
			else
			{
				log._xmlSocket.send(cmd);
			}
		}
	}
}